/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.db.mysql;

import org.ajiaojr.enigmagm.client.data.User;
import org.ajiaojr.enigmagm.client.service.DataAccessException;
import org.ajiaojr.enigmagm.db.ConnectionManager;
import org.ajiaojr.enigmagm.db.UserDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MySQLUserDAOImpl implements UserDAO {
  public User login(String username, String password)
      throws DataAccessException {
    User result = null;

    Connection c = null;

    try {
      c = ConnectionManager.getInstance().getConnection();

      PreparedStatement s =
          c
              .prepareStatement("SELECT `privilege` FROM user WHERE username = ? AND password = SHA1(?) AND enabled = 1");
      s.setString(1, username.toLowerCase());
      s.setString(2, password);

      ResultSet rs = s.executeQuery();

      if (rs.next()) {
        result = new User(username, rs.getLong(1));

        rs.close();
        s.close();

      }

      return result;

    } catch (Exception e) {
      throw new DataAccessException(e.getMessage());
    } finally {
      try {
        c.close();
      } catch (SQLException e) {
        throw new DataAccessException(e.getMessage());
      }
    }
  }

  public User loginViaAuthToken(String username, String authToken)
      throws DataAccessException {
    User u = null;

    Connection c = null;

    try {
      c = ConnectionManager.getInstance().getConnection();

      PreparedStatement s =
          c
              .prepareStatement("SELECT `privilege` FROM user WHERE username = ? AND auth_token = ? AND enabled = 1");
      s.setString(1, username.toLowerCase());
      s.setString(2, authToken);

      ResultSet rs = s.executeQuery();

      if (rs.next()) {
        u = new User(username, rs.getLong(1));

        rs.close();
        s.close();

      }
      return u;
    } catch (Exception e) {
      throw new DataAccessException(e.getMessage());
    } finally {
      try {
        c.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }

  public void register(String username, String password)
      throws DataAccessException {
    Connection c = null;

    try {
      c = ConnectionManager.getInstance().getConnection();

      PreparedStatement s =
          c
              .prepareStatement("INSERT INTO user (username, password) VALUES ( ? , SHA1( ? ))");
      s.setString(1, username.toLowerCase());
      s.setString(2, password);

      s.execute();

      s.close();

    } catch (Exception e) {
      throw new DataAccessException(e.getMessage());
    } finally {
      try {
        c.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }

  /**
   * @param username
   * @param authToken
   */
  public void saveAuthToken(String username, String authToken)
      throws DataAccessException {
    Connection c = null;
    try {
      c = ConnectionManager.getInstance().getConnection();

      PreparedStatement s =
          c
              .prepareStatement("UPDATE `user` SET auth_token = ? WHERE username = ?");
      s.setString(1, authToken);
      s.setString(2, username);

      s.executeUpdate();

      s.close();
    } catch (Exception e) {
      throw new DataAccessException(e.getMessage());
    } finally {
      try {
        c.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
}
